<template>
  <view class="container">
    <TopBtn :title="'问诊建议'"></TopBtn>
    <view class="info">
      <patientCard :data="data"></patientCard>
      <view class="textAreaBox">
        <view class="title">
          <text class="red">*</text>
          咨询诉求
        </view>
        <textArea v-model="updata.appeal" :inputNane="updata.appeal" :maxNum="500"></textArea>
        <view class="title mar_Top">
          <text class="red">*</text>
          问诊建议
        </view>
        <textArea v-model="updata.proposal" :inputNane="updata.proposal" :maxNum="200"></textArea>
      </view>
    </view>
    <view class="Btn" @click="createConsultationAdvice()" v-if="data.consultationAdviceRespVOList.length == 0">
      {{ this.updata.id ? '修改' : '提交' }}
    </view>
  </view>
</template>

<script>
import {
  SendGroupMessage
} from '@/utils/TIM'
import {
  getPatientInfoByOutId,
  createConsultationAdvice,
  getConsultationAdvice,
  updateConsultationAdvice,
} from '@/api/ConsultationRoom'
export default {
  data () {
    return {
      groupId: '',
      id: '',
      patientId: '',
      doctorId: '',
      value: '',
      data: { consultationAdviceRespVOList: [] },
      updata: {
        appeal: '',
        proposal: '',
      },
    }
  },
  onLoad (options) {
    this.id = options.id
    this.patientId = options.patientId
    this.doctorId = options.doctorId
    this.groupId = options.groupId
    this.getPatientInfoByOutId()
    if (options.updata && options.updata != 0) {
      this.getConsultationAdvice(options.updata)
    }
  },
  methods: {
    getConsultationAdvice (e) {
      getConsultationAdvice({
        id: e,
      }).then(res => {
        this.updata = res.data
      })
    },
    createConsultationAdvice () {
      this.updata.outpatientId = this.id
      this.updata.patientId = this.data.patientId
      if (!this.updata.appeal) {
        uni.showToast({
          title: '咨询诉求',
          duration: 2000,
          icon: 'none',
        })
        return
      }
      if (!this.updata.proposal) {
        uni.showToast({
          title: '问诊建议',
          duration: 2000,
          icon: 'none',
        })
        return
      }
      uni.showLoading({
        title: '提交中',
        mask: true,
      })
      if (this.updata.id) {
        updateConsultationAdvice(this.updata).then(res => {
          // 发送群消息
          this.sendMessage(this.updata.proposal)

          uni.showToast({
            title: '操作成功',
            duration: 200,
            icon: 'none',
            mask: true,
          })
          setTimeout(() => {
            uni.hideLoading()
            uni.$emit('show', true)
            uni.navigateBack({
              delta: 1,
            })
          }, 200)
        })
      } else {
        createConsultationAdvice(this.updata).then(res => {
          // 发送群消息
          this.sendMessage(this.updata.proposal)

          uni.showToast({
            title: '操作成功',
            duration: 200,
            icon: 'none',
            mask: true,
          })
          setTimeout(() => {
            uni.hideLoading()
            uni.$emit('show', true)
            uni.navigateBack({
              delta: 1,
            })
          }, 200)
        })
      }
    },
    getPatientInfoByOutId () {
      getPatientInfoByOutId({
        outpatientId: this.id,
      }).then(res => {
        this.data = res.data
        this.updata.appeal = res.data.consultationAdviceRespVOList[0].appeal
        this.updata.proposal = res.data.consultationAdviceRespVOList[0].proposal
      })
    },
    sendMessage (text) {
      // 发送群消息
      const message = {
        businessID: 'tip',
        type: 'card',
        title: '问诊建议',
        text,
        buttonText: '去查看',
        buttonUrl: {
          patient: `/pagesCommon/consultation/GoConsultation?id=${this.id}&patientId=${this.patientId}&doctorId=${this.doctorId}`,
          doctor: `/pages/ConsultationRoom/proposal/proposal?id=${this.id}`,
        },
      }
      SendGroupMessage(this.groupId, message)
    },
  },
}
</script>

<style lang="less" scoped>
.Btn {
  width: 649rpx;
  height: 100rpx;
  margin: 68rpx auto;
  background: #3b6cfe;
  border-radius: 50rpx;
  font-size: 28rpx;
  color: #ffffff;
  text-align: center;
  line-height: 100rpx;
}

.info {
  margin-top: 30rpx;

  .mar_Top {
    margin-top: 30rpx;
  }

  .textAreaBox {
    margin-top: 30rpx;
    width: 750rpx;
    min-height: 633rpx;
    background: #ffffff;
    border-radius: 20rpx;
    padding: 30rpx 50rpx 50rpx;
    box-sizing: border-box;
    font-size: 32rpx;
    color: #333333;

    text {
      margin-right: 6rpx;
    }

    .title {
      margin-bottom: 20rpx;
      font-weight: bold;
    }
  }
}
</style>
